﻿namespace Ruirun.Mes.EntityConfiguration
{
    public class MaterialCategoryConfiguration : EntityTypeConfigurationBase<MaterialCategory, Guid>
    {
        /// <summary>
        /// 获取 所属的数据上下文类型
        /// </summary>
        public override Type DbContextType => typeof(MesDbContext);

        /// <summary>
        /// 重写以实现实体类型各个属性的数据库配置
        /// </summary>
        /// <param name="builder">实体类型创建器</param>
        public override void Configuration(EntityTypeBuilder<MaterialCategory> builder)
        {
            builder.HasIndex(m => new { m.Name, m.ParentId }).SetIndex("Name", true);
            builder.HasIndex(m => m.ParentId).SetIndex("ParentId", false);

            switch (DbContextOptions?.DatabaseType)
            {
                case DatabaseType.SqlServer:
                case DatabaseType.PostgreSql:
                case DatabaseType.Oracle:
                    builder.HasIndex(m => m.TreePathString).SetIndex("TreePathString", true);
                    break;
            }

            builder.HasOne(m => m.Parent).WithMany(m => m.Children).HasForeignKey(m => m.ParentId).SetForeignKey("ParentId", false);

        }

    }
}
